package org.istt.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * User entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "TBL_USER", schema = "dbo", catalog = "onegate")
public class User extends BaseObject<Long> implements java.io.Serializable {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = -760277302709288524L;
	private Set<FileManagement> approvers = new HashSet<FileManagement>();
	private Date dateCreated;
	private Date DateModified;
	private Department department;
	private String email;
	private Boolean enabled;
	private Set<Exchange> exchanges = new HashSet<Exchange>(0);
	private String firstName;
	private Set<Group> groups = new HashSet<Group>(0);
	private Long id;
	private Date lastLogin;
	private String lastName;
	private String password;
	private String phone;
	private Position position;
	private Set<FileManagement> receiverRejectFiles = new HashSet<FileManagement>();
	private Set<FileManagement> senders = new HashSet<FileManagement>();
	private String tel;
	private String username;

	// Constructors

	/** default constructor */
	public User() {
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
        if (!(o instanceof User)) return false;
        final User user = (User) o;
        return getUsername().equals(user.getUsername());
	}

	/**
	 * @return the approvers
	 */
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "approver")
	public Set<FileManagement> getApprovers() {
		return approvers;
	}

	@Column(name = "DATE_CREATED")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getDateCreated() {
		return dateCreated;
	}

	@Column(name = "DATE_MODIFIED")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getDateModified() {
		return DateModified;
	}

	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY)
	@JoinColumn(name="DEP_ID")
	public Department getDepartment() {
		return department;
	}

	@Column(name = "EMAIL", length = 50)
	public String getEmail() {
		return this.email;
	}

	@Column(name = "ENABLED")
	public Boolean getEnabled() {
		return enabled;
	}

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="user")
	public Set<Exchange> getExchanges() {
		return this.exchanges;
	}

	@Column(name = "FIRST_NAME")
	public String getFirstName() {
		return this.firstName;
	}

	@Transient
	public String getFullname() {
		String result = "";
		if(getFirstName()!=null && getLastName()!=null ){
			result = new StringBuilder(getFirstName())
			.append(" ")
			.append(getLastName()).toString() ; 
		}else if(getFirstName()!=null){
			result = getFirstName(); 
		}else if(getLastName()!=null){
			result = getLastName(); 
		}
		return result;
	}

	@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
	@JoinTable(name = "TBL_GROUP_USER", schema = "dbo", catalog = "onegate", joinColumns = { @JoinColumn(name = "USER_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "GROUP_ID", nullable = false, updatable = false) })
	public Set<Group> getGroups() {
		return this.groups;
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "ID", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	@Column(name = "LAST_LOGIN", length = 23)
	public Date getLastLogin() {
		return this.lastLogin;
	}

	@Column(name = "LAST_NAME")
	public String getLastName() {
		return this.lastName;
	}

	@Column(name = "PASSWORD", length = 250)
	public String getPassword() {
		return this.password;
	}

	@Column(name = "PHONE", length = 15)
	public String getPhone() {
		return this.phone;
	}
	
	@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY)
	@JoinColumn(name="POSITION_ID")
	public Position getPosition() {
		return position;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "receiverRejectFile")
	public Set<FileManagement> getReceiverRejectFiles() {
		return receiverRejectFiles;
	}
	
	/**
	 * @return the senders
	 */
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "sender")
	public Set<FileManagement> getSenders() {
		return senders;
	}

	@Column(name = "TEL", length = 15)
	public String getTel() {
		return this.tel;
	}

	@Column(name = "USERNAME", length = 30)
	public String getUsername() {
		return this.username;
	}

	@Override
	public int hashCode() {
		return getUsername().hashCode();
	}

	/**
	 * @param approvers the approvers to set
	 */
	public void setApprovers(Set<FileManagement> approvers) {
		this.approvers = approvers;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public void setDateModified(Date dateModified) {
		DateModified = dateModified;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}
	
	public void setExchanges(Set<Exchange> exchanges) {
		this.exchanges = exchanges;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public void setPosition(Position position) {
		this.position = position;
	}

	public void setReceiverRejectFiles(Set<FileManagement> receiverRejectFiles) {
		this.receiverRejectFiles = receiverRejectFiles;
	}

	/**
	 * @param senders the senders to set
	 */
	public void setSenders(Set<FileManagement> senders) {
		this.senders = senders;
	}
	
	public void setTel(String tel) {
		this.tel = tel;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public String toString() {
		 return  "User ('" + getId() + "'), " +
         					"Username: '" + getUsername() + "'";
	}

}